home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / rovers / ProblemManager / ProblemManager.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-29  |  12.5 KB  |  366 lines

  1. /*    *****************************************************************
  2.     *    ProblemManager.c - Problem Management Routines        *
  3.     *****************************************************************
  4.  
  5.     ProblemManager( Cmd, TypeProblem, nodename, UniqueID, StatusLine )
  6.         Cmd - One of ADD_PROBLEM, ADD_PRIMARY_PROBLEM, DELETE_PROBLEM
  7.         TypeProblem - Name of TestName that Failed
  8.         nodename - Textual Name of Node ( Not-necesarily unique )
  9.         UniqueId - Usually an IP UniqueIDess or otherwise unique Problem ID
  10.         StatusLine - If Cmd is UPDATE_PROBLEM
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <time.h>
  16. #include "ProblemManager.h"    /* Problem Manager routine defines */
  17. #include "version.h"
  18. /*#define DEBUG 1 /* */
  19.  
  20. static char *DefaultPingkyDir=".";   /* Starting point in File System */
  21. static char *PingkyDir;           /* Starting point in File System */
  22.  
  23. static char Problem_File[100];        /* PROBLEM FILE path */
  24. static long ProblemFileLastUpdated;    /* Last time PROBLEM.FILE was updated */
  25. struct ProblemType 
  26.     ProblemArray[MAXPROBLEMS];/* In Core version of existing problems*/
  27. int NumProblems=0;
  28.  
  29. static char Log_File[100];             /* Where do we log things */
  30.  
  31. static char Config_File[100];          /* Where does User Comment Out Nodes */
  32. static long ConfigFileLastUpdated;    /* Last time Config File was accessed */
  33.  
  34. static int ProblemFileChanged=0;
  35.  
  36.     /************************************************
  37.      * FORWARD DECLARATION OF STATIC ROUTINES    *
  38.      ************************************************/
  39. static int Add_Problem_Entry();
  40. static int AddProblem();
  41. static int DelProblem();
  42. static void ReadConfigFile();
  43. static char *Comment();
  44. #ifdef STANDALONE
  45. char *programname;
  46. #endif
  47.  
  48. /****************************************************************
  49.  *   Problem_Manager:                         *
  50.  *    Cmds:                            *
  51.  *        ADD_PROBLEM:  Add a problem to the list        *
  52.  *                ( If not already there )    *
  53.  *        ADD_PRIMARY_PROBLEM: Add a problem deleteing    *
  54.  *            all other problems with this node     *
  55.  *        DELETE_PROBLEM: Remove this problem        *
  56.  *                ( if there is one )        *
  57.  *        UPDATE_PROBLEM: Update this problem's StatusLine*
  58.  *                ( if there is one )        *
  59.  *        CLAIM_PROBLEM: Claim this problem        *
  60.  *                ( just like UPDATE PROBLEM )    *
  61.  *                                  *
  62.  ****************************************************************/
  63. int Problem_Manager(Cmd,TypeProblem,nodename,UniqueID,StatusLine)
  64. int Cmd;        /* Add_Problem, Del_Problem, Add_Problem_Primary*/
  65. char *TypeProblem;    /* Type of Problem - string text        */
  66. char *nodename;        /* nodename of node we are reporting about     */
  67. char *UniqueID;        /* Unique Identifier for this node(like IP UniqueID)*/
  68. char *StatusLine;    /* Status Line - if Cmd is UPDATE_PROBLEM    */
  69. {
  70. int i,changed=0;
  71. char buffer[100];
  72. extern char *getenv();
  73. extern char *programname;
  74. long TimeNow;
  75. static int virgin=1;
  76.  
  77.     ProblemFileChanged=0;  /* We haven't changed the PROBLEM.FILE this call */
  78.     if ( virgin ) {    /* Set-Up Config Files we will access */
  79.         if ((PingkyDir=getenv("PINGKYDIR"))==NULL) 
  80.         PingkyDir=DefaultPingkyDir;
  81.         strcpy(Problem_File,PingkyDir); strcat(Problem_File ,"/PROBLEM.FILE");
  82.         strcpy(Log_File,PingkyDir); strcat(Log_File,"/problemlog");
  83.         strcpy(Config_File,PingkyDir); strcat(Config_File,"/ProblemManager.config");
  84.     /*sprintf(buffer,"%s: %s\n",programname,Version);
  85.     printf(buffer);
  86.     Log( buffer, Log_File );*/
  87.     virgin=0;
  88.    }
  89.  
  90.    /************ ReRead Config File if it has changed **********/
  91.    if ( FileChanged( Config_File, &ConfigFileLastUpdated )     ) {
  92.     ReadConfigFile( Config_File );
  93.     FileChanged( Config_File, &ConfigFileLastUpdated );
  94.    }
  95.    /************* ReRead Problem File if it has changed **********/
  96.    if ( FileChanged( Problem_File, &ProblemFileLastUpdated )     ) {
  97.       NumProblems = ReadProblemFile( ProblemArray, Problem_File );
  98.       FileChanged( Problem_File, &ProblemFileLastUpdated );
  99.     ProblemFileChanged=1;
  100.    }
  101.    /****************** Handle Caller's Command *******************/
  102.    switch( Cmd ) {
  103.     case ADD_PRIMARY_PROBLEM: 
  104.         changed=0;
  105.              for (i=0; i<NumProblems ; i++)
  106.                    if (strcmp(ProblemArray[i].UniqueID,UniqueID)==0)
  107.                       if (strcmp(ProblemArray[i].TestName,TypeProblem)!=0){
  108.                       ProblemArray[i].Name[0]='\0';
  109.                        changed=1;
  110.                 }
  111.         if ( changed ) {
  112.           WriteProblemFile( ProblemArray,Problem_File,NumProblems);
  113.           NumProblems=ReadProblemFile(ProblemArray,Problem_File);
  114.           ProblemFileChanged=1;
  115.         }
  116.     case ADD_PROBLEM: 
  117.               NumProblems=AddProblem(nodename,UniqueID,TypeProblem,
  118.                      ProblemArray,NumProblems);
  119.               break;
  120.     case DELETE_PROBLEM: 
  121.                  NumProblems=DelProblem(nodename,UniqueID,TypeProblem,
  122.                      ProblemArray, NumProblems);
  123.               break;
  124.     case UPDATE_PROBLEM: 
  125.     case CLAIM_PROBLEM: 
  126.             i=Problem_Exists(UniqueID,TypeProblem,ProblemArray,NumProblems);
  127.  
  128.             if ( i != -1 ) {
  129.                 strncpy( ProblemArray[i].StatusLine, StatusLine, MAXSTATUSLINE-1 );
  130.                 ProblemArray[i].StatusLine[MAXSTATUSLINE-1]='\0';
  131.                    WriteProblemFile(ProblemArray,Problem_File,NumProblems); 
  132.                   NumProblems=ReadProblemFile(ProblemArray,Problem_File);
  133.                 if ( Cmd == UPDATE_PROBLEM )
  134.                      sprintf(buffer," ^%s^%s^Operator Update: %s (%d mins elapsed)\n",
  135.                     ProblemArray[i].Name,ProblemArray[i].UniqueID,ProblemArray[i].StatusLine,(time(&TimeNow)-ProblemArray[i].TimeStamp)/60);
  136.                 else
  137.                      sprintf(buffer," ^%s^%s^Operator Claim: %s (%d mins elapsed)\n",
  138.                     ProblemArray[i].Name,ProblemArray[i].UniqueID,ProblemArray[i].StatusLine,(time(&TimeNow)-ProblemArray[i].TimeStamp)/60);
  139.                      Log(buffer,Log_File);
  140.                 ProblemFileChanged=1;
  141.  
  142.             }
  143.               break;
  144.     case CHECK:
  145.             break;
  146.     default:  
  147.         Log("Problem_Manager- invalid cmd type!\n");
  148.         panic("Problem_Manager- invalid cmd type!\n");
  149.           break;
  150.    }
  151.    return(ProblemFileChanged);
  152. }
  153.  
  154.  
  155. /**************************************************************************
  156.  * Add_Problem_Entry:  Add a problem to the Problem structure and to disk *
  157.  * To Do: Open this file with the append flag                             *
  158.  **************************************************************************/ 
  159. static int Add_Problem_Entry(Node,UniqueID,ProblemType,Problem, NumProblems)
  160. char *Node,*UniqueID,*ProblemType;
  161. struct ProblemType *Problem;
  162. int NumProblems;
  163. {
  164.    time( &Problem[NumProblems].TimeStamp );
  165.    strncpy( Problem[NumProblems].Name, Node, MAXNODENAME-1 ); 
  166.     Problem[NumProblems].Name[MAXNODENAME-1]='\0';
  167.    strncpy( Problem[NumProblems].UniqueID, UniqueID, MAXUNIQUEID-1 );
  168.     Problem[NumProblems].UniqueID[MAXUNIQUEID-1]='\0';
  169.    strncpy( Problem[NumProblems].TestName, ProblemType, MAXTESTNAME-1 );
  170.     Problem[NumProblems].TestName[MAXTESTNAME-1]='\0';
  171.    strncpy( Problem[NumProblems].StatusLine, Comment( Node ), MAXSTATUSLINE-1 );
  172.     Problem[NumProblems].StatusLine[MAXSTATUSLINE-1]='\0';
  173. /*   printf("Add_Problem_Entry: ADDING %lu %s %s %s_%s_%s %s\n",
  174.         Problem[NumProblems].TimeStamp,_Day(Problem[NumProblems].TimeStamp),
  175.         _Time(Problem[NumProblems].TimeStamp),
  176.         Problem[NumProblems].Name,Problem[NumProblems].UniqueID,
  177.          Problem[NumProblems].TestName, Problem[NumProblems].StatusLine);*/
  178.    NumProblems++;
  179.    WriteProblemFile( Problem , Problem_File, NumProblems );
  180.    NumProblems=ReadProblemFile(ProblemArray,Problem_File);
  181.    ProblemFileChanged=1;
  182.    return( NumProblems );
  183. }
  184.  
  185. /********************************************************************
  186.  * AddProblem:   Add a problem to the Problem File if necessary     *
  187.  *                                      *
  188.  *  Variables:                                *
  189.  *    Node: The name of the node that is having problems.        *
  190.  *      UniqueID: The IP UniqueIDess of the node that is having the problem *
  191.  *    Service: The service that is failing (PING,TELNET,etc)        *
  192.  *    Problem_File: Name of the Problem File containing Problems  *
  193.  ********************************************************************/
  194. static int AddProblem(Node,UniqueID,Service,Problem,NumProblems)
  195. char *Node,*UniqueID;
  196. char * Service;
  197. struct ProblemType *Problem;
  198. int NumProblems;
  199. {
  200. char Event[100];
  201.  
  202.    if ( TestNode( Node ) ) {
  203. /*    printf("ProblemManager: %s is a test node - not added\n",Node);*/
  204.     return( NumProblems );
  205.    }
  206.    if ( Problem_Exists(UniqueID,Service,Problem,NumProblems) == -1 ) {  
  207.            sprintf(Event,"ADDING PROBLEM %s_%s_%s_%s\n",
  208.         Node,UniqueID,Service,Comment(Node));
  209.            Log(Event,Log_File);    
  210.         NumProblems=Add_Problem_Entry(Node,UniqueID,Service,Problem,NumProblems);
  211.    }
  212.    return( NumProblems );
  213. }
  214.  
  215. /********************************************************************
  216.  * DelProblem:   Delete a problem in the Problem File if necessary  *
  217.  *                                      *
  218.  *  Variables:                                *
  219.  *    Node: The name of the node that is now behaving.        *
  220.  *      UniqueID: The IP UniqueIDess of the node that is behaving correctly *
  221.  *    Service: The service that works now (PING,TELNET,etc)        *
  222.  *    Problem_File: Name of the Problem File containing Problems  *
  223.  ********************************************************************/
  224. static int DelProblem(Node,UniqueID,Service,Problem,NumProblems)
  225. char *Node,*UniqueID;
  226. char *Service;
  227. struct ProblemType *Problem;
  228. int NumProblems;
  229. {
  230. char Event[100];
  231. int index;
  232. long TimeNow;
  233.  
  234.    time(&TimeNow);
  235.  
  236.    if  ( (index=Problem_Exists(UniqueID,Service,Problem,NumProblems)) != -1 ) {
  237.           sprintf(Event,"DELETING PROBLEM %s_%s_%s (%d mins elapsed)\n",
  238.         Problem[index].Name,
  239.         Problem[index].UniqueID,
  240.             Service,((TimeNow-Problem[index].TimeStamp)/60));
  241.       Log(Event,Log_File);
  242.                     /* DELETE the problem */
  243.       Problem[index].Name[0]='\0';    /* Mark this node invalid */
  244.       WriteProblemFile(Problem,Problem_File,NumProblems);
  245.       NumProblems=ReadProblemFile(Problem,Problem_File);
  246.       ProblemFileChanged=1;
  247.    }
  248.    return( NumProblems );
  249. }
  250.  
  251. /************************************************************************
  252.  *    AUTOMATIC STATUS MESSAGES / TEST NODE SECTION            *
  253.  ************************************************************************/
  254. #define MAXNOTES MAXNODES
  255. #define MAXCOMMENT 30
  256.  
  257. static int NumConfig=0;
  258. struct ConfigType {
  259.     char ICare;
  260.     char Name[MAXNODENAME];
  261.     char Comment[MAXCOMMENT];
  262. } Config[MAXNOTES];
  263.  
  264. /****** ReadConfigFile - Default comments, and TestName Nodes ******/
  265. /*    NODE COMMENT LINE FOR ALL TO SEE    BEV            */
  266. static void ReadConfigFile( Config_File )
  267. char *Config_File;
  268. {
  269. FILE *stream;
  270. char buffer[100];
  271. int i=0,j,k;
  272.  
  273.  
  274.    if ((stream=fopen(Config_File,"r")) == NULL) 
  275.     panic("ReadConfig: config file unreadable!\n");
  276.  
  277.    while( ( fgets(buffer,sizeof(buffer),stream) != NULL) && ( i < MAXNOTES ) ) {
  278.       if (buffer[0]=='#') 
  279.         continue; 
  280.     j=0;
  281.     if ( buffer[j] == '*' ) {    /* '*' in Col 1 means IGNORE */
  282.         Config[i].ICare=0;
  283.         j++;
  284.     }
  285.     else Config[i].ICare=1;        /* No '*' means I Care &*/
  286.       for(k=0; j<MAXNODENAME && !isspace(buffer[j]); j++,k++)
  287.     Config[i].Name[k]=buffer[j];
  288.       Config[i].Name[MAXNODENAME-1]='\0';
  289.  
  290.       for(j++,k=0; j<strlen(buffer) && k<MAXCOMMENT; k++,j++ )
  291.      if ( isprint( buffer[j] ) ) 
  292.          Config[i].Comment[k]=buffer[j];
  293.       Config[i].Comment[ MAXCOMMENT-1 ] = '\0';
  294.  
  295. /*    printf("Name[%d]=%s Comment=%s ICare=%d\n",i,Config[i].Name,Config[i].Comment,Config[i].ICare);*/
  296.       i++;              /** Yea! We have a valid line **/
  297.    }
  298.    fclose(stream);
  299.    NumConfig=i;
  300. }
  301.  
  302. static int IsConfig( node )
  303. char *node;
  304. {
  305. int i;
  306.  
  307.    if (FileChanged(Config_File,&ConfigFileLastUpdated)) {
  308.       ReadConfigFile( Config_File );
  309.       FileChanged(Config_File,&ConfigFileLastUpdated);
  310.    }
  311.     for( i=0; i<NumConfig; i++ ) {
  312.         if ( strcmp( Config[i].Name , node ) == 0 )
  313.             if ( Config[i].ICare == 0 ) {
  314.                 return( -1 );    /* TestNode Machine -I don't care */
  315.             }
  316.             else return( i );    /* INDEX OF NODE */
  317.         else;
  318.     }
  319.     return( -2 ); /* NODE NOT FOUND */
  320. }
  321.  
  322. int TestNode( node )
  323. char *node;
  324. {
  325.     if ( IsConfig( node ) == -1 ) return( 1 );    /* TestNode */
  326.     else return( 0 );    /* Non-TestNode */
  327. }
  328.  
  329. static char *Comment( node )
  330. char *node;
  331. {
  332. int i;
  333.  
  334.     if ( ( i=IsConfig( node ) ) == -2 ) {
  335.         /*printf("No Comment for %s\n",node);*/
  336.         return( "" ); 
  337.     }
  338.     else {
  339.         /*printf("%s Comment=%s\n",node, Config[i].Comment );*/
  340.         return( Config[i].Comment );
  341.     }
  342. }
  343.  
  344. #ifdef STANDALONE
  345. main(argc, argv)
  346. int argc;
  347. char *argv[];
  348. {
  349.     programname=argv[0];
  350.     Problem_Manager(ADD_PROBLEM,"PING","NODE1","NODE1ID",NULL);
  351.     Problem_Manager(ADD_PROBLEM,"PING","NODE2","NODE2ID",NULL);
  352.     Problem_Manager(ADD_PROBLEM,"PING","NODE3","NODE3ID",NULL);
  353.     PrintProblems(NumProblems,ProblemArray);
  354.     sleep(10);
  355.     Problem_Manager(DELETE_PROBLEM,"PING","NODE2","NODE2ID",NULL);
  356.     PrintProblems(NumProblems,ProblemArray);
  357.     sleep(10);
  358.     Problem_Manager(ADD_PRIMARY_PROBLEM,"DOWN","NODE1","NODE1ID",NULL);
  359.     PrintProblems(NumProblems,ProblemArray);
  360.     sleep(10);
  361.     Problem_Manager(DELETE_PROBLEM,"PING","NODE3","NODE3ID",NULL);
  362.     Problem_Manager(UPDATE_PROBLEM,"PING","NODE1","NODE1ID","AN UPDATE");
  363.     PrintProblems(NumProblems,ProblemArray);
  364. }
  365. #endif
  366.